﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.ZWQK;
using StateGrid.GlobalVariables;

public partial class EditSYML : System.Web.UI.Page
{
    /// <summary>
    /// 当前索引目录的索引编码
    /// </summary>
    private String currentSYBM = null;
    
    /// <summary>
    /// 当前索引目录
    /// </summary>
    private ZWQK_SYML zwqk_syml = null;

    /// <summary>
    /// 操作对象
    /// </summary>
    private ZWQK_DAO zwqkDAO = new ZWQK_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!UserManageDAO.IsLogin((User)Session["login"]))
            {
                GlobalMethod.RedirectLogin(Response);
                return;
            }

        if (!this.IsPostBack)
        {
                this.BindData();
        }
    }

    /// <summary>
    /// 更新索引目录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submitButton_Click(object sender, EventArgs e)
    {
        this.zwmcLabel.Text = "";
        if (null == this.zwmcTextBox.Text || "".Equals(this.zwmcTextBox.Text.Trim()))
        {
            this.zwmcLabel.Text = "错误：中文名称不能为空";
            return;
        }
        ZWQK_SYML zwqkSYML = this.GetZWQK_SYML();
        if (this.zwqkDAO.updateZwqkSYMLBySYBM(zwqkSYML))
        {
            //Response.Redirect(GlobalChineseJournal.LISTCHINESEJOURNALCATALOG_PAGE+"?currentCatalog="+zwqkSYML.flbm);
            Response.Write("<Script>alert('修改中文期刊目录成功！')</Script>");
        }
        else
        {
            String message = "错误：修改中文期刊索引目录失败";
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message="+message);
            return;
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        this.currentSYBM=(String)Request["sybm"];
        if (null == this.currentSYBM)
        {
            String message = "错误：索引编码为空";
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message="+message);
            return;
        }
        this.zwqk_syml = this.zwqkDAO.getZwqkSYMLBySybm(this.currentSYBM);

        if (null == this.zwqk_syml)
        {
            String message = "错误：没有找到索引编码对应的索引期刊";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        //索引编码
        this.sybmTextBox.Text = this.zwqk_syml.sybm;
        //分类编码
        this.flbmTextBox.Text = this.zwqk_syml.flbm;
        //分类名称
        this.SetFLMC(this.zwqk_syml.flbm);
        //收录年份
        this.slnfTextBox.Text = this.zwqk_syml.slnf;
        //年份区间
        this.nfqjTextBox.Text = this.zwqk_syml.nfqj.ToString();
        //最新年份
        this.zxnfTextBox.Text = this.zwqk_syml.zxnf.ToString();
        //中文名称
        this.zwmcTextBox.Text = this.zwqk_syml.zwmc;
        //开放
        if (this.zwqk_syml.kf)
        {
            this.kfCheckBox.Checked = true;
        }
        else
        {
            this.kfCheckBox.Checked = false;
        }
        //外文名称
        this.wwmcTextBox.Text = this.zwqk_syml.wwmc;
        //主办单位
        this.zbdwTextBox.Text = this.zwqk_syml.zbdw;
        //创刊年份
        this.cknfTextBox.Text = this.zwqk_syml.cknf;
        //编辑单位
        this.bjdwTextBox.Text = this.zwqk_syml.bjdw;
        //负责人
        this.fzrTextBox.Text = this.zwqk_syml.fzr;
        //通讯地址
        this.txdzTextBox.Text = this.zwqk_syml.txdz;
        //邮政编码
        this.yzbmTextBox.Text = this.zwqk_syml.yzbm;
        //办公电话
        this.bgdhTextBox.Text = this.zwqk_syml.bgdh;
        //传真电话
        this.czdhTextBox.Text = this.zwqk_syml.czdh;
        //电子邮件
        this.dzyjTextBox.Text = this.zwqk_syml.dzyj;
        //发行周期
        this.SetFXZQ(this.zwqk_syml.fxzq);
        //国际刊号
        this.gjkhTextBox.Text = this.zwqk_syml.gjkh;
        //国内刊号
        this.gnkhTextBox.Text = this.zwqk_syml.gnkh;
        //资料来源
        this.zllyTextBox.Text = this.zwqk_syml.zlly;
        //邮发代号
        this.yfdhTextBox.Text = this.zwqk_syml.yfdh;
        //简介
        this.jjTextBox.Text = this.zwqk_syml.jj;
        
    }

    /// <summary>
    /// 重新绑定数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }

    /// <summary>
    /// 设置发行周期
    /// </summary>
    /// <param name="fxzq">发行周期</param>
    private void SetFXZQ(String fxzq)
    {
        if (null == fxzq)
        {
            return;
        }

        if (fxzq.Equals("半年刊"))
        {
            this.fxzqDropDownList.SelectedIndex = 0;
            return;
        }
        if (fxzq.Equals("季刊"))
        {
            this.fxzqDropDownList.SelectedIndex = 1;
            return;
        }
        if (fxzq.Equals("双月刊"))
        {
            this.fxzqDropDownList.SelectedIndex = 2;
            return;
        }
        if (fxzq.Equals("月刊"))
        {
            this.fxzqDropDownList.SelectedIndex = 3;
            return;
        }
        if (fxzq.Equals("半月刊"))
        {
            this.fxzqDropDownList.SelectedIndex = 4;
            return;
        }
        if (fxzq.Equals("周刊"))
        {
            this.fxzqDropDownList.SelectedIndex = 5;
            return;
        }
    }


    /// <summary>
    /// 根据分类编码设置分类名称
    /// </summary>
    /// <param name="flbm">分类编码</param>
    private void SetFLMC(String flbm)
    {
        //获取分类
        ZWQK_FL zwqkFL = this.zwqkDAO.getZwqkFLByFlbm(flbm);

        if (null == zwqkFL)
        {
            return;
        }
        //设置分类名称
        this.flmcTextBox.Text = zwqkFL.flmc;
    }

    /// <summary>
    /// 获取页面中的信息
    /// </summary>
    /// <returns>一个索引目录对象</returns>
    private ZWQK_SYML GetZWQK_SYML()
    {
        ZWQK_SYML zwqkSYML = new ZWQK_SYML();

        //分类编码
        zwqkSYML.flbm = this.flbmTextBox.Text.Trim();
        //索引编码
        zwqkSYML.sybm = this.sybmTextBox.Text.Trim();
        //中文名称
        zwqkSYML.zwmc = this.zwmcTextBox.Text.Trim();
        //外文名称
        zwqkSYML.wwmc = this.wwmcTextBox.Text.Trim();
        //开放
        if (this.kfCheckBox.Checked)
        {
            zwqkSYML.kf = true;
        }
        else
        {
            zwqkSYML.kf = false;
        }

        //收录年份
        zwqkSYML.slnf = this.slnfTextBox.Text.Trim();

        //最新年份
        try
        {
            zwqkSYML.zxnf = Int32.Parse(this.zxnfTextBox.Text.Trim());
        }
        catch (Exception)
        {
        }

        //年份区间
        try
        {
            zwqkSYML.nfqj = Int32.Parse(this.nfqjTextBox.Text.Trim());
        }
        catch (Exception)
        {
        }
        //主办单位
        zwqkSYML.zbdw = this.zbdwTextBox.Text.Trim();
        //编辑单位
        zwqkSYML.bjdw = this.bjdwTextBox.Text.Trim();
        //通讯地址
        zwqkSYML.txdz = this.txdzTextBox.Text.Trim();
        //办公电话
        zwqkSYML.bgdh = this.bgdhTextBox.Text.Trim();
        //传真电话
        zwqkSYML.czdh = this.czdhTextBox.Text.Trim();
        //电子邮件
        zwqkSYML.dzyj = this.dzyjTextBox.Text.Trim();
        //创刊年份
        zwqkSYML.cknf = this.cknfTextBox.Text.Trim();
        //负责人
        zwqkSYML.fzr = this.fzrTextBox.Text.Trim();
        //邮政编码
        zwqkSYML.yzbm = this.yzbmTextBox.Text.Trim();
        //发行周期
        zwqkSYML.fxzq = this.fxzqDropDownList.SelectedValue;
        //国际刊号
        zwqkSYML.gjkh = this.gjkhTextBox.Text.Trim();
        //国内刊号
        zwqkSYML.gnkh = this.gnkhTextBox.Text.Trim();
        //邮发代号
        zwqkSYML.yfdh = this.yfdhTextBox.Text.Trim();
        //资料来源
        zwqkSYML.zlly = this.zllyTextBox.Text.Trim();
        //简介
        zwqkSYML.jj = this.jjTextBox.Text;

        return zwqkSYML;
    }
}